package cn.b.sky.mes.qm.controller;

import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import cn.b.sky.mes.qm.model.DefectType;
import cn.b.sky.mes.qm.model.condition.DefectTypeCondition;
import cn.b.sky.mes.qm.service.DefectTypeService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.b.sky.common.constants.SystemConstant;
import cn.b.sky.common.model.BatchJobReport;
import cn.b.sky.common.model.DWZResult;
import cn.b.sky.user.controller.SecurityController;

/**
 * 
 * <strong>Title : DefectTypeController.java<br></strong>
 * <strong>Package : cn.b.sky.mes.qm.controller<br></strong>
 * <strong>Description : </strong>缺陷类型控制器<br> 
 * <strong>Create on : 2015-12-14 上午10:37:47<br></strong>
 * <p>
 * @author dapeng.li<br>
 * @version <strong>v1.0.0</strong><br>
 * <br>
 * <strong>修改历史:</strong><br>
 * 修改人 | 修改日期 | 修改描述<br>
 * -------------------------------------------<br>
 * <br>
 * <br>
 */
@Controller
@RequestMapping("/defectType/*")
public class DefectTypeController extends SecurityController {

  private static final String DEFECT_TYPE_LIST_PAGE = "qm/defectType/defectType_list";

  private static final String DEFECT_TYPE_PAGE = "qm/defectType/defectType_add";

  @Autowired
  DefectTypeService defectTypeService;

  @RequestMapping("defectType_list")
  public String selectDefectTypeListByCondition(String navTabId, Model model) {
    model.addAttribute(NAVTABID, navTabId);
    return DEFECT_TYPE_LIST_PAGE;
  }

  @RequestMapping("selectDefectTypeList")
  @ResponseBody
  public DWZResult selectDefectTypeList(@ModelAttribute("condition") DefectTypeCondition condition) {
    condition.setSiteCode(getSiteCode());
    List<DefectType> list = defectTypeService.selectDefectTypeListByCondition(condition);
    int count = defectTypeService.selectDefectTypeCountByCondition(condition);
    condition.setTotalCount(count);
    condition.setData(list);
    return ajaxDone(condition);
  }

  @RequestMapping("addDefectType")
  public String addDefectType(String navTabId, Model model) {
    model.addAttribute(NAVTABID, navTabId);
    model.addAttribute("pattern", "add");

    return DEFECT_TYPE_PAGE;
  }

  @RequestMapping("modifyDefectType")
  public String modifyDefectType(String id, String navTabId, Model model) {
    DefectType defectType = defectTypeService.getDefectType(getSiteCode(), id);

    model.addAttribute(NAVTABID, navTabId);
    model.addAttribute("defectType", defectType);
    model.addAttribute("pattern", "modify");

    return DEFECT_TYPE_PAGE;
  }

  @RequestMapping("viewDefectType")
  public String viewDefectType(String id, String navTabId, Model model) {
    DefectType defectType = defectTypeService.getDefectType(getSiteCode(), id);

    model.addAttribute(NAVTABID, navTabId);
    model.addAttribute("defectType", defectType);
    model.addAttribute("pattern", "view");

    return DEFECT_TYPE_PAGE;
  }

  @RequestMapping("saveDefectType")
  @ResponseBody
  public DWZResult saveDefectType(String id, DefectType defectType, String navTabId, Model model) {
    if (null == defectType || StringUtils.isBlank(defectType.getDefectTypeCode())) {
      return ajaxError(getImeViewMessageI18n(null,
          SystemConstant.PARAMETERNULL));
    }

    defectType.setSiteCode(getSiteCode());
    setCreateInfo(defectType);
    if (defectType.getLockFlag() == null) {
      defectType.setLockFlag(0);
    }
    if (StringUtils.isBlank(id)) {
      defectTypeService.insertDefectType(defectType);
    } else {
      defectTypeService.updateDefectType(defectType);
    }
    return dialogAjaxDone(
        getImeViewMessageI18n(null, "successfulOperation.M"),
        navTabId, null, null, null);
  }

  @RequestMapping("deleteDefectType")
  @ResponseBody
  public DWZResult deleteDefectType(String navTabId, Model model, HttpServletRequest request) {
    String ids = request.getParameter("ids");
    if (StringUtils.isBlank(ids)) {
      return ajaxError(getImeViewMessageI18n(null,
          SystemConstant.PARAMETERNULL));
    }

    String[] idArr = ids.split(",");
    BatchJobReport[] reports = new BatchJobReport[idArr.length];
    int failures = 0;
    for (int i = 0; i < idArr.length; i++) {
      reports[i] = defectTypeService.deleteDefectType(getSiteCode(), idArr[i]);
      if (!reports[i].isSuccess()) {
        failures += 1;
      }
    }

    if (failures == 0) {
      return navTabAjaxDone(navTabId, null, null, null,
          getImeViewMessageI18n(null, "successfulOperation.M"),
          null);
    } else {
      return batchjobAjaxDone( request, Arrays.asList(reports));
    }
  }

  @RequestMapping("validateDefectTypeCode")
  @ResponseBody
  public boolean validateDefectTypeCode(String defectTypeCode) {
    if (StringUtils.isBlank(defectTypeCode)) {
      return true;
    } else {
      DefectType defectType = defectTypeService.getDefectType(getSiteCode(), defectTypeCode);
      return null == defectType;
    }
  }

  @RequestMapping("defectTypeDropdown")
  @ResponseBody
  public List<DefectType> defectTypeDropdown(String defectTypeCode) {
    DefectTypeCondition condition = new DefectTypeCondition();
    condition.setSiteCode(getSiteCode());
    condition.setDefectTypeCode(defectTypeCode);
    condition.setLockFlag(0);
    return defectTypeService.selectDefectTypeForSuggest(condition);
  }

  @RequestMapping("checkDefectTypeCode")
  @ResponseBody
  public boolean checkDefectTypeCode(String defectTypeCode) {
    DefectType defectType = defectTypeService.getDefectType(getSiteCode(), defectTypeCode);
    return defectType != null && defectType.getLockFlag() != 1;
  }

  @RequestMapping("defectTypeLookup")
  public String defectTypeLookup(@ModelAttribute("condition") DefectTypeCondition condition, String navTabId,
      Model model) {
    condition.setSiteCode(getSiteCode());
    condition.setLockFlag(0);
    List<DefectType> list = defectTypeService.selectDefectTypeListByCondition(condition);
    int count = defectTypeService.selectDefectTypeCountByCondition(condition);

    condition.setTotalCount(count);
    condition.setData(list);

    model.addAttribute(NAVTABID, navTabId);
    model.addAttribute("page", condition);
    return "qm/defectType/defectType_lookup";
  }
}
